﻿using System.Collections.Generic;
using System.Text;

namespace tiny_compiler
{
	public class Compare :
		IOperator
	{
		public Compare(string _valueLeftName, string _valueLeftOffsetName, string _valueRightName, string _label, bool _equal)
		{
			m_valueLeftName = _valueLeftName;
			m_valueLeftOffsetName = _valueLeftOffsetName;
			m_valueRightName = _valueRightName;
			m_labelName = _label;
			m_equal = _equal;
		}

		#region IOperator Members

		public int Length
		{
			get { return Compiler.NameLength + Compiler.LengthLength + (Compiler.NumberLength * 4) + (Compiler.LengthLength * 2) + 1; }
		}

		public string Name
		{
			get { return string.Empty; }
		}

		public void Resolve(IEnumerable<IOperator> _operators)
		{
			m_valueLeft = _operators.Offset(m_valueLeftName);
			m_valueLeftOffset = _operators.Offset(m_valueLeftOffsetName);
			m_valueRight = _operators.Offset(m_valueRightName);
			m_label = _operators.Offset(m_labelName);
			m_valueRightLength = _operators.Operator(m_valueRightName).Length;
		}

		public void Append(StringBuilder _data)
		{
			_data.Append(Compiler.CompareName);
			_data.Append(CompilerUtility.ConvertLength(Length));
			_data.Append(CompilerUtility.ConvertNumber(m_valueLeft));
			_data.Append(CompilerUtility.ConvertNumber(m_valueLeftOffset));
			_data.Append(CompilerUtility.ConvertNumber(m_valueRight));
			_data.Append(CompilerUtility.ConvertNumber(m_label));
			_data.Append(CompilerUtility.ConvertLength(m_valueRightLength));
			_data.Append(CompilerUtility.ConvertLength(m_equal ? 1 : 0));
			_data.Append(" ");
		}

		#endregion

		private string m_valueLeftName;
		private int m_valueLeft;
		private string m_valueLeftOffsetName;
		private int m_valueLeftOffset;
		private string m_valueRightName;
		private int m_valueRight;
		private string m_labelName;
		private int m_label;
		private bool m_equal;
		private int m_valueRightLength;
	}
}
